package com.animee.tally.util;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import androidx.annotation.NonNull;
import com.animee.tally.R;
/* 在记录界面的弹出时间窗口时，进行使用*/
public class SelectTimeDialog extends Dialog implements View.OnClickListener {
    EditText hourEt,minuteEt;
    DatePicker datePicker;
    Button ensureBtn,cancelBtn;
    public interface OnEnsureListener{
        public void onEnsure(String time,int year,int month,int day);
    }
    OnEnsureListener onEnsureListener;     // 声明接口
    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }
    public SelectTimeDialog(@NonNull Context context) {
        super(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_time);
        //查找布局当中需要使用的控件
        hourEt = findViewById(R.id.dialog_time_et_hour);
        minuteEt = findViewById(R.id.dialog_time_et_minute);
        datePicker = findViewById(R.id.dialog_time_dp);
        ensureBtn = findViewById(R.id.dialog_time_btn_ensure);
        cancelBtn = findViewById(R.id.dialog_time_btn_cancel);
        ensureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        hideDatePickerHeader(datePicker);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_time_btn_cancel:
                cancel();    //取消当前对话框
                break;
            case R.id.dialog_time_btn_ensure:
                if (onEnsureListener!=null) {
                    //获取当前的时间
                    int year = datePicker.getYear();
                    int month = datePicker.getMonth()+1;
                    String monthstr = month+"";
                    if (month<10){
                        monthstr="0"+month;
                    }
                    int day = datePicker.getDayOfMonth();
                    String daystr = day+"";
                    if (day<10){
                        daystr = "0"+day;
                    }
                    // 获取输入的小时和分钟
                    String hourstr =hourEt.getText().toString().trim();
                    String minutestr =minuteEt.getText().toString().trim();
                    int hour =0;
                    if (!TextUtils.isEmpty(hourstr)) {
                        hour =  Integer.parseInt(hourstr);
                    }
                   int minute = 0;
                    if (!TextUtils.isEmpty(minutestr)) {
                        minute = Integer.parseInt(minutestr);
                    }
                    hour=hour%24;
                    minute=minute%60;

                    hourstr =""+hour;
                    minutestr = ""+minute;
                    if(hour<10){
                        hourstr="0"+hour;
                    }
                    if(minute<10){
                        minutestr="0"+minute;
                    }
                    String time = year+"年"+monthstr+"月"+daystr+"日 "+hourstr+":"+minutestr;
                    onEnsureListener.onEnsure(time,year,month,day);
                    cancel();
                }
                break;
        }
    }
    // 隐藏DatePicker的头部的方法
    private void hideDatePickerHeader(DatePicker datePicker) {
        ViewGroup rootView = (ViewGroup) datePicker.getChildAt(0);
        if (rootView == null) {
            return;
        }
        View headerView = rootView .getChildAt(0);
        if (headerView == null) {
            return;
        }
        //5.0+
        int headerId = getContext().getResources().getIdentifier("day_picker_selector_layout", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);

            ViewGroup.LayoutParams layoutParamsRoot = rootView.getLayoutParams();
            layoutParamsRoot.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            rootView.setLayoutParams(layoutParamsRoot);

            ViewGroup animator = (ViewGroup) rootView.getChildAt(1);
            ViewGroup.LayoutParams layoutParamsAnimator = animator.getLayoutParams();
            layoutParamsAnimator.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            animator.setLayoutParams(layoutParamsAnimator);

            View child = animator.getChildAt(0);
            ViewGroup.LayoutParams layoutParamsChild = child.getLayoutParams();
            layoutParamsChild.width = ViewGroup.LayoutParams.WRAP_CONTENT;
            child.setLayoutParams(layoutParamsChild );
            return;
        }
        //6.0+
        headerId = getContext().getResources().getIdentifier("date_picker_header", "id", "android");
        if (headerId == headerView.getId()) {
            headerView.setVisibility(View.GONE);
        }
    }
}
